package com.anti.modular.sys.emp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import com.anti.jdbc.DBUtils;
import com.anti.modular.sys.auth.pojo.LoginEmpInfo;
import com.anti.modular.sys.emp.entity.SysEmp;
import com.anti.modular.sys.emp.param.SysEmpParam;
import com.anti.modular.sys.emp.result.SysEmpInfo;
import com.anti.modular.sys.emp.service.SysEmpExtOrgPosService;
import com.anti.modular.sys.emp.service.SysEmpPosService;
import com.anti.modular.sys.emp.service.SysEmpService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2021-3-11
 */
@Service
public class SysEmpServiceImpl implements SysEmpService {
    @Resource
    DBUtils dbUtils;

    @Resource
    SysEmpPosService sysEmpPosService;
    @Resource
    SysEmpExtOrgPosService sysEmpExtOrgPosService;


    @Override
    public boolean hasOrgEmp(Long orgId) {
        String sql="select count(*) from sys_emp where org_id =? ";
        Integer integer = dbUtils.getFirst(sql, Integer.class, orgId);
        return integer >= 1;
    }

    @Override
    public void updateEmpOrgInfo(Long orgId, String orgName) {
        String sql="update sys_emp set org_name=? where org_id=? ";
        dbUtils.execSql(sql,orgName,orgId);
    }

    @Override
    public SysEmpInfo getSysEmpInfo(Long empId) {
        SysEmpInfo sysEmpInfo = new SysEmpInfo();
        //获取员工信息
        SysEmp sysEmp = dbUtils.getFirst("select * from sys_emp where id =? ",SysEmp.class,empId);
        if (ObjectUtil.isNotNull(sysEmp)) {
            BeanUtil.copyProperties(sysEmp, sysEmpInfo);
            //获取附属机构和职位信息
            List<Dict> empExtOrgPosDictList = sysEmpExtOrgPosService.getEmpExtOrgPosDictList(sysEmp.getId(), true);
            sysEmpInfo.setExtOrgPos(empExtOrgPosDictList);

            //获取所属职位信息
            List<Dict> empEmpPosDictList = sysEmpPosService.getEmpPosDictList(sysEmp.getId(), true);
            sysEmpInfo.setPositions(empEmpPosDictList);
        } else {
            sysEmpInfo.setExtOrgPos(CollectionUtil.newArrayList());
            sysEmpInfo.setPositions(CollectionUtil.newArrayList());
        }
        return sysEmpInfo;
    }

    @Override
    public void addOrUpdate(SysEmpParam sysEmpParam) {
        Long empId = sysEmpParam.getId();
        SysEmp sysEmp = dbUtils.getFirst("select * from sys_emp where id =?",SysEmp.class,empId);
        if (ObjectUtil.isNull(sysEmp)) {
            sysEmp = new SysEmp();
        }
        BeanUtil.copyProperties(sysEmpParam, sysEmp);
        this.saveOrUpdate(sysEmp);
        //编辑附属机构职位相关信息
        List<Dict> extIdList = sysEmpParam.getExtIds();
        sysEmpExtOrgPosService.addOrEdit(empId, extIdList);
        //编辑职位相关信息
        List<Long> posIdList = sysEmpParam.getPosIdList();
        sysEmpPosService.addOrEdit(empId, posIdList);
    }

    @Override
    public void deleteEmpInfoByUserId(Long empId) {
        //删除员工信息
        String deleteSql="delete from sys_emp where emp_id=?";
        dbUtils.execSql(deleteSql,empId);
        //级联删除对应的员工-附属信息
        sysEmpExtOrgPosService.deleteEmpExtInfoByUserId(empId);
        //级联删除对用的员工-职位信息
        sysEmpPosService.deleteEmpPosInfoByUserId(empId);
    }

    @Override
    public LoginEmpInfo getLoginEmpInfo(Long empId) {
        LoginEmpInfo loginEmpInfo = new LoginEmpInfo();
        //获取员工信息
        String sql="select * from sys_emp where id=?";
        SysEmp sysEmp = dbUtils.getFirst(sql, SysEmp.class,empId);
        if (ObjectUtil.isNotNull(sysEmp)) {
            BeanUtil.copyProperties(sysEmp, loginEmpInfo);
            //获取附属机构和职位信息
            List<Dict> empExtOrgPosDictList = sysEmpExtOrgPosService.getEmpExtOrgPosDictList(sysEmp.getId(), false);
            loginEmpInfo.setExtOrgPos(empExtOrgPosDictList);

            //获取所属职位信息
            List<Dict> empEmpPosDictList = sysEmpPosService.getEmpPosDictList(sysEmp.getId(), false);
            loginEmpInfo.setPositions(empEmpPosDictList);
        } else {
            loginEmpInfo.setExtOrgPos(CollectionUtil.newArrayList());
            loginEmpInfo.setPositions(CollectionUtil.newArrayList());
        }
        return loginEmpInfo;
    }

    void saveOrUpdate(SysEmp sysEmp){
        String sql="select count(*) from sys_emp where id=?";
        Integer first = dbUtils.getFirst(sql, Integer.class, sysEmp.getId());
        if (first!=0){
            String updateSql="update sys_emp set job_num=?,org_id=?,org_name=? where id =? ";
            dbUtils.execSql(updateSql,sysEmp.getJobNum(),sysEmp.getOrgId(),sysEmp.getOrgName(),sysEmp.getId());
        }else {
            String insertSql="insert into sys_emp (id,job_num,org_id,org_name) values (?,?,?,?)";
            dbUtils.execSql(insertSql,sysEmp.getId(),sysEmp.getJobNum(),sysEmp.getOrgId(),sysEmp.getOrgName());
        }
    }
}
